package com.kibey.tools;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by mchwind on 18/9/13.
 */

public class Utils {

    public static void main(String[] args) throws IOException {
        String str = "<string name=\"with_draw_amount\">提现金额</string>";
        str = "    <string name=\"app_name\">PluginMitc</string>";
        System.out.println(getStringsName(str));
        System.out.println(getStringsValue(str));
    }

    public static String getStringsName(String string) {
        try {
            String regex = "^(<string name=\").*(\">)";
            Matcher matcher = Pattern.compile(regex).matcher(string.trim());
            while (matcher.find()) {
                String name = matcher.group();
                String[] strs = name.split("name=\"");
                if (strs.length >= 2) {
                    return strs[1].replaceAll("\">", "");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getStringsValue(String string) {
        try {
            string = string.trim();
            String start = "<string name=\"" + getStringsName(string) + "\">";
            String end = "</string>";
            string = string.replaceAll(start, "");
            string = string.replaceAll(end, "");
            return string;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static List<StringsEntry> strings2Entry(File file) {
        java.util.List<String> list = null;
        List<StringsEntry> result = new ArrayList<>();
        try {
            list = FileUtils.readLines(file, "UTF-8");
            for (String s : list) {
                String key = Utils.getStringsName(s);
                String value = Utils.getStringsValue(s);

                if (null != key) {
//                    System.out.println(key + "\t" + value);

                    result.add(new StringsEntry(key, value));
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }


    public static Map<String, String> getStrings4Map(File file) {
        return getStrings4Map(file, true);
    }

    public static Map<String, String> getStrings4Map(File file, boolean keyValueOrValueKey) {
        java.util.List<String> list = null;
        HashMap<String, String> map = new HashMap<>();
        try {
            list = FileUtils.readLines(file, "UTF-8");
            for (String s : list) {
                String key = Utils.getStringsName(s);
                String value = Utils.getStringsValue(s);

                if (null != key) {
//                    System.out.println(key + "\t" + value);
                    if (keyValueOrValueKey) {
                        map.put(key, value);
                    } else {
                        map.put(value, key);
                    }
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }

    public static String getRootFilePath() {
        return new File("").getAbsolutePath();
    }

    public static File getResFile(String name) {
        String file = getResRoot() + name;
        return new File(file);
    }

    private static String getResRoot() {
        return new File("").getAbsolutePath() + "/src/res/";
    }

    public static File getStringFile(String language) {
        return Utils.getResFile("strings", "strings_" + language + ".xml");
    }

    public static File getOldStringFile(String language) {
        return Utils.getResFile("strings_old", "strings_" + language + ".xml");
    }

    public static File getResFile(String dir, String name) {
        String filePath = getResRoot() + dir + "/" + name;
        File file = new File(filePath);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdir();
        }
        return file;
    }

    public static String getAndroidStringItem(String key, String values) {
        String stringItem = "<string name=\"" + key + "\">" + values + "</string>\n";
        return stringItem;
    }

    public static CharSequence getAndroidStringItem(Map.Entry<String, String> entry) {
        return getAndroidStringItem(entry.getKey(), entry.getValue());
    }

    public static class StringsEntry implements Map.Entry<String, String> {
        public String key;
        public String value;

        public StringsEntry(String key, String value) {
            this.key = key;
            this.value = value;
        }

        @Override
        public String getKey() {
            return key;
        }

        @Override
        public String getValue() {
            return value;
        }

        @Override
        public String setValue(String value) {
            return null;
        }
    }
}


